#!/usr/bin/env python
# encoding: utf-8
"""
converter.py

Copyright (c) 2008 Patrick J Leyden III

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

import sys
import os
import datetime

from datastore import *

from google.appengine.ext import db

class MemberInfo:
    def __init__(self, lname, fname, email, date):
        self.lname = lname
        self.fname = fname
        self.email = email
        self.date = date
        
class SimpleConverter:
    # objects should be a list of keys.
    def to_text(self, keys):
        elements = db.get(keys)
        text = ""
        for element in elements:
            text += "%s, %s, %s, %s\n" % (element.lname,
                                         element.fname,
                                         element.email,
                                         element.date)
        return text
    
    # text should get converted to a list of keys that have
    # been added to the database.
    def to_keys(self, text):
        keys = []
        lines = text.splitlines()
        for line in lines:
            split_line = map(lambda a: a.strip(), line.split(","))
            if len(split_line) == 4:
                # TODO: fix this to parse the time string.
                new_date = self.to_date(split_line[3])
                new_duty = Duty(fname = split_line[1],
                                lname = split_line[0],
                                email = split_line[2],
                                date = new_date)
                new_key = new_duty.put()
                keys.append(new_key)
            else:
                print "Failed to parse line."
        return keys
        
    def to_date(self, str):
        new_date = datetime.date.today()
        pieces = str.split("/")
        if len(pieces) != 3:
            pieces = str.split("-")
        if len(pieces) == 3:    
            year = int(pieces[0])
            month = int(pieces[1])
            day = int(pieces[2])
            new_date = datetime.date(year, month, day)
        return new_date
            

def test1():
    print "Testing converter.py"
    
    test_text = '''last name, first name, email, date
                   last name, first name, email, date
                   last name, first name, email, date'''
    lines = test_text.splitlines()
    members = []
    for line in lines:
        split_line = map(lambda a: a.strip(), line.split(","))
        if len(split_line) == 4:
            info = MemberInfo(split_line[0], split_line[1], split_line[2], split_line[3])
            members.append(info)
        else:
            print "Failed to parse line."
    print "Printing %d members:" % len(members)
    for member in members:
        print "last name:", member.lname
        print "first name:", member.fname
        print "email:", member.email
        print "date:", member.date
        
test_text2 = """
Leyden, Patrick, pleyden@gmail.com, 07/31/08
Shmo, Joe, jshmo@gmail.com, 08/10/08
"""
        
def test2():
    test_text = '''Leyden, Patrick, pleyden@gmail.com, 7/31/08
                   Shmo, Joe, jshmo@gmail.com, 8/04/08'''
    sc = SimpleConverter()
    keys = sc.to_keys(text)
    print "="*30
    print "keys"
    print "="*30
    for key in keys:
        print key
    text = sc.to_text(keys)
    print "="*30
    print "text"
    print "="*30
    print text


if __name__ == '__main__':
    test2()